package com.sky.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.constant.DefaultAdressConstant;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.exception.AddAddressBookFailedException;
import com.sky.exception.AddressBookBusinessException;
import com.sky.exception.UpdateAddressBookFailedException;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import com.sky.mapper.AddressBookMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author luo20
 * @description 针对表【address_book(地址簿)】的数据库操作Service实现
 * @createDate 2025-07-22 13:24:10
 */
@Service
@Slf4j
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook>
        implements AddressBookService {
    @Autowired
    private AddressBookMapper addressBookMapper;

    @Override
    public void addAddress(AddressBook addressBook) {
        Integer saveResult = addressBookMapper.saveAddress(addressBook);
        if (saveResult <= 0) {
            throw new AddAddressBookFailedException(MessageConstant.ADD_ADDRESS_BOOK_FAILED);
        }
    }

    @Override
    public List<AddressBook> showAllAddressBook() {
        // 获取userId
        Long userId = BaseContext.getCurrentId();
        return addressBookMapper.list(userId);
    }

    @Override
    public void setDefaultAddress(Integer id) {
        // 查询上一个默认地址
        AddressBook oldAddressBook = addressBookMapper.getDeafaultAddress(new AddressBook().setIsDefault(DefaultAdressConstant.IS_DEFAULT_ADDRESS));
        // 将上一个默认地址设为非默认
        if (ObjectUtils.isNotEmpty(oldAddressBook)) {
            AddressBook addressBookConditions = new AddressBook()
                    .setIsDefault(DefaultAdressConstant.NOT_DEFAULT_ADDRESS)
                    .setId(oldAddressBook.getId());
            Integer oldAddressResult = addressBookMapper.updDefaultStatusAddress(addressBookConditions);
            if (oldAddressResult <= 0) {
                throw new UpdateAddressBookFailedException(MessageConstant.CANCEL_DEFAULT_ADDRESS_BOOK_FAILED);
            }
        }
        // 设置新默认地址值
        // 创建addressBook对象
        AddressBook newAddressBook = new AddressBook()
                // 设置默认地址
                .setIsDefault(DefaultAdressConstant.IS_DEFAULT_ADDRESS)
                // 设置地址id
                .setId(Long.valueOf(id));
        Integer updateResult = addressBookMapper.updDefaultStatusAddress(newAddressBook);
        if (updateResult <= 0) {
            throw new UpdateAddressBookFailedException(MessageConstant.UPDATE_DEFAULT_ADDRESS_BOOK_FAILED);
        }
    }

    @Override
    public AddressBook getDefaultAddress() {
        AddressBook addressBook = addressBookMapper.getDeafaultAddress(new AddressBook()
                .setIsDefault(DefaultAdressConstant.IS_DEFAULT_ADDRESS)
                .setUserId(BaseContext.getCurrentId()));
        if (ObjectUtils.isEmpty(addressBook)) {
            throw new UpdateAddressBookFailedException(MessageConstant.GET_DEFAULT_ADDRESS_FAILD);
        }
        return addressBook;
    }

    @Override
    public void updAddress(AddressBook addressBook) {
        Integer updAddressResult = addressBookMapper.updAddressBook(addressBook);
        if (updAddressResult <= 0) {
            throw new UpdateAddressBookFailedException(MessageConstant.UPDATE_ADDRESS_FAILED);
        }
    }

    @Override
    public void removeAddressById(Integer id) {
        // 先查询是否是默认地址
        AddressBook addressBook = addressBookMapper.getDeafaultAddress(new AddressBook().setId(Long.valueOf(id)));

        if (ObjectUtils.isNotEmpty(addressBook)) {
            // 如果是默认地址，则不能删除
            if (Objects.equals(addressBook.getIsDefault(), DefaultAdressConstant.IS_DEFAULT_ADDRESS)) {
                throw new AddressBookBusinessException(MessageConstant.DELETE_DEFAULT_ADDRESS_ERROR);
            }
        } else {
            throw new AddressBookBusinessException(MessageConstant.ADDRESS_NOT_EXIST);
        }
        // 不是就删除
        Integer result = addressBookMapper.delAddressById(id);
        if (result <= 0){
            throw new AddressBookBusinessException(MessageConstant.DELETE_ADDRESS_FAILED);
        }
    }
}




